<?php

class volume_model extends CI_Model 
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	// Get Volume based on Access ID
	function getVolumeByID( $access_id )
	{
		$this->db->where('access_id', $access_id);
		$this->db->from('tbl_volume');
		return $this->db->get();
	}
	
	// Get Volumes based on array of Access IDs
	function getVolumesByID( $access_id_array )
	{
		$this->db->where('access_id', '0');
	
		foreach ($access_id_array as $access_id)
			$this->db->or_where('access_id', $access_id);
			
		$this->db->from('tbl_volume');
		$this->db->order_by('title', 'asc');
		return $this->db->get();
	}
	
	// Get last Access ID
	function getLastAccessID( )
	{
		$this->db->select_max('access_id');
		return $this->db->get('tbl_volume')->row()->access_id;
	}

	// Delete volume based on Access ID
	function deleteVolumeByID( $access_id )
	{
		$delete_where_array = array('access_id' => $access_id );
	
		$this->db->delete('tbl_volume', $delete_where_array );
	}
	
	// Add volume
	function addVolume( $data_array )
	{
		$this->db->insert('tbl_volume', $data_array); 
	}
	
	// Updates volumes based on Access ID
	function updateVolume( $access_id, $data_array )
	{
		$update_where_array = array('access_id' => $access_id );
	
		$this->db->update('tbl_volume', $data_array, $update_where_array); 
	}

	// Search Volume Table
	function getSearchResults( $search_by='title', $terms='ALL', $offset=0, $limit=20 )
	{
		// If we do not want all results
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $word)
				$this->db->like($search_by, $word);
		}
		$this->db->from('tbl_volume');
		$this->db->limit($limit, $offset);
		$this->db->order_by($search_by, 'asc');
		return $this->db->get();
	}
	
	// Get Total Number of Results for Search
	function getSearchCount( $search_by='title', $terms='ALL' )
	{
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $key=>$word)
				$this->db->like($search_by, $word);
		}
		$this->db->from('tbl_volume');
		return $this->db->count_all_results();
	}
}

?>